FROM <%= from %>
LABEL cide=""
LABEL cide.build.complete="false"

USER root
RUN useradd -m -U -d <%= CIDE_DIR %> cide

# Install system build dependencies here

<% if as_root -%>
  <% as_root.add.each do |file| -%>
ADD <%= file.src.join(' ') %> <%= file.dest %>
  <% end -%>
  <% as_root.env.each_pair do |key, value| -%>
ENV <%= key %> <%= value %>
  <% end -%>
  <% as_root.run.each do |cmd| -%>
RUN <%= cmd %>
  <% end -%>
<% end -%>

# Common

ENV HOME <%= CIDE_DIR %>
WORKDIR <%= CIDE_SRC_DIR %>

# SSH config

<% if use_ssh -%>
RUN mkdir <%= CIDE_SSH_DIR %>
RUN echo StrictHostKeyChecking no > <%= File.join(CIDE_SSH_DIR, 'config') %>
RUN chmod 400 <%= File.join(CIDE_SSH_DIR, 'config') %>

ADD <%= TEMP_SSH_KEY %> <%= File.join(CIDE_SSH_DIR, 'id_rsa') %>
RUN chmod 400 <%= File.expand_path('id_rsa', CIDE_SSH_DIR) %>
RUN chown -R cide:cide <%= CIDE_DIR %>
<% end -%>

# Before

<% if before -%>
  <% before.add.each do |file| -%>
ADD <%= file.src.join(' ') %> <%= file.dest %>
  <% end %>
RUN chown -R cide:cide <%= CIDE_DIR %>
  <% before.env.each_pair do |key, value| -%>
ENV <%= key %> <%= value %>
  <% end %>
USER cide
  <% before.run.each do |cmd| -%>
RUN <%= cmd %>
  <% end %>
<% end -%>

# Add project data

USER root
ADD . <%= CIDE_SRC_DIR %>
RUN chown -R cide:cide <%= CIDE_DIR %>
USER cide
LABEL cide.build.complete="true"
